package com.cloud.wms.ui.inside.replenish;

import android.os.Message;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatCheckBox;

import com.cloud.core.config.EventConfig;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.ReplenishApplyContact;
import com.cloud.wms.ui.inside.entity.RecordGoodsEntity;
import com.cloud.wms.ui.inside.presenter.ReplenishApplyPresenter;

import butterknife.BindView;

/**
 * Created by FC on 2021/3/12.
 * Desc：补货申请确认
 */
public class ApplyConfirmActivity extends BaseBarActivity<ReplenishApplyPresenter> implements ReplenishApplyContact.View {
    @BindView(R.id.et_amount)
    EditText etAmount;
    @BindView(R.id.tv_commodity_name)
    TextView tvCommodityName;
    @BindView(R.id.tv_bar_code)
    TextView tvBarCode;
    @BindView(R.id.tv_stock_type)
    TextView tvStockType;
    @BindView(R.id.tv_pos_code)
    TextView tvPosCode;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    @BindView(R.id.cb_urgent)
    AppCompatCheckBox cbUrgent;
    private RecordGoodsEntity recordGoodsEntity;

    @Override
    protected String barTitle() {
        return "补货申请";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_replenish_apply_confirm;
    }

    @Override
    protected void initEvent() {
        etAmount.requestFocus();
        recordGoodsEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        tvCommodityName.setText(recordGoodsEntity.getCommodityName());
        tvBarCode.setText(recordGoodsEntity.getBarCode());
        tvStockType.setText(recordGoodsEntity.getStockType() == 1 ? "良品" : "次品");
        tvPosCode.setText(recordGoodsEntity.getPosCode());
        tvAmount.setText(String.valueOf(recordGoodsEntity.getAmount()));
        btnConfirm.setOnClickListener(v -> {
            if (TextUtils.isEmpty(etAmount.getText().toString().trim())) {
                error("数量不能为空");
                return;
            }
            if (Integer.parseInt(etAmount.getText().toString().trim()) <= 0) {
                error("请输入正确的补货数量");
                return;
            }
            mPresenter.confirmReplenish(recordGoodsEntity
                    , etAmount.getText().toString().trim(), cbUrgent.isChecked() ? 1 : 0);
        });
    }

    @Override
    public void onSuccess(Message message) {
        if (message.what == ReplenishApplyPresenter.CONFIRM_REPLENISH_SUCCESS) {
            success("提交补货申请成功");
            EventBusUtil.sendEvent(new Event(EventConfig.ADD_RECORD_SUCCESS));
            finish();
        }
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
